<?php require_once('../Connections/learnConn.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "admin";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_learnConn, $learnConn);
$query_kelas = "SELECT * FROM kelas WHERE kelas = '7'";
$kelas = mysql_query($query_kelas, $learnConn) or die(mysql_error());
$row_kelas = mysql_fetch_assoc($kelas);
$totalRows_kelas = mysql_num_rows($kelas);



if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO siswa (nis, nama, jenis_kelamin, tempat_lahir, tanggal_lahir, agama, alamat, nama_ayah, pekerjaan_ayah, nama_ibu, pekerjaan_ibu, alamat_ortu, tlp_ortu, nama_wali, pekerjaan_wali, alamat_wali, tlp_wali, golongan_darah, jum_saudara, anak_ke, asal_sekolah, no_sttb, tahun_ajaran) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['nis'], "text"),
                       GetSQLValueString($_POST['nama'], "text"),
                       GetSQLValueString($_POST['jenis_kelamin'], "text"),
                       GetSQLValueString($_POST['tempat_lahir'], "text"),
                       GetSQLValueString($_POST['tanggal_lahir'], "text"),
                       GetSQLValueString($_POST['agama'], "text"),
                       GetSQLValueString($_POST['alamat'], "text"),
                       GetSQLValueString($_POST['nama_ayah'], "text"),
                       GetSQLValueString($_POST['pekerjaan_ayah'], "text"),
                       GetSQLValueString($_POST['nama_ibu'], "text"),
                       GetSQLValueString($_POST['pekerjaan_ibu'], "text"),
                       GetSQLValueString($_POST['alamat_ortu'], "text"),
                       GetSQLValueString($_POST['tlp_ortu'], "text"),
                       GetSQLValueString($_POST['nama_wali'], "text"),
                       GetSQLValueString($_POST['pekerjaan_wali'], "text"),
                       GetSQLValueString($_POST['alamat_wali'], "text"),
                       GetSQLValueString($_POST['tlp_wali'], "text"),
                       GetSQLValueString($_POST['golongan_darah'], "text"),
                       GetSQLValueString($_POST['jum_saudara'], "text"),
                       GetSQLValueString($_POST['anak_ke'], "text"),
                       GetSQLValueString($_POST['asal_sekolah'], "text"),
                       GetSQLValueString($_POST['no_sttb'], "text"),
                       GetSQLValueString($_POST['tahun_ajaran'], "text"));
	$insertSQL2 = sprintf("INSERT INTO siswa_kelas (nis, kd_kelas, kd_matpel) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['nis'], "text"),
					   GetSQLValueString($_POST['kelas'], "text"),
					   GetSQLValueString(" ", "text")
					   );
  mysql_select_db($database_learnConn, $learnConn);
  $Result1 = mysql_query($insertSQL, $learnConn) or die(mysql_error());
  $Result2 = mysql_query($insertSQL2, $learnConn) or die(mysql_error());

  $insertGoTo = "siswa.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

				$date = getdate();
				$tahun = $date['year'];
				$tahun2 = $date['year']+1;
				$colname_Recordset1 = "-1";

$colname_Recordset1 = $tahun;
mysql_select_db($database_learnConn, $learnConn);
$query_Recordset1 = sprintf("SELECT nis FROM siswa WHERE tahun_ajaran = %s ORDER BY nis DESC", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $learnConn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tambah Siswa</title>
<link href="../style/style.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
//-->

function autoNIS() {
	var fil = <?php echo substr($tahun, -2); ?>;
	var fil2 = <?php echo substr($tahun2, -2); ?>;
	var urutan = <?php echo substr($row_Recordset1['nis'], -3); ?> + 1;
	if (urutan < 10) {
		urutan="00"+urutan;
	}
	else if (urutan < 100 && urutan >9) {
		urutan="0"+urutan;
	}
	var tahun = fil + "" +fil2+"07"+urutan;
	document.form1.nis.value = tahun;
}
</script>
<link href="../style/pro_drop_1.css" type="text/css" rel="stylesheet"/>
<script src="../style/stuHover.js" type="text/javascript"></script>
</head>

<body><center>
<table width="900" bgcolor="#FFFFFF">
	<?php
	include "topmenu_admin.php";
	?>
    <tr>
    	<td id="menu">Admin Menu :
      <ul>
        	<li><a href="index.php">Home</a></li>
            <li><a href="siswa.php">Siswa</a></li>
            <li><a href="pegawai.php">Pegawai</a></li>
            <li><a href="matpel.php">Mata Pelajaran</a></li>
            <li><a href="jadwal.php">Jadwal</a></li>
            <li><a href="pengumuman.php">Pengumuman</a></li>
            <li><a href="buku_tamu.php">Buku Tamu</a></li>
            <li><a href="../logout.php">Logout</a></li>
        </ul>
        </td>
    	<td id="konten">&nbsp;
          <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1" onsubmit="MM_validateForm('nis','','R','nama','','R','tempat_lahir','','R','tanggal_lahir','','R','alamat','','R','nama_ayah','','R','pekerjaan_ayah','','R','nama_ibu','','R','pekerjaan_ibu','','R','alamat_ortu','','R','tlp_ortu','','R','nama_wali','','R','pekerjaan_wali','','R','alamat_wali','','R','tlp_wali','','R','jum_saudara','','R','anak_ke','','R','asal_sekolah','','R','no_sttb','','R','tahun_ajaran','','R');return document.MM_returnValue">
            <table align="center">
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tahun Masuk :</td>
                <td>
               <input type="hidden" value="<?php echo $tahun; ?>" name="tahun_ajaran" /><?php echo $tahun."/".$tahun2; ?></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Kelas :</td>
                <td><select name="kelas" id="kelas">
                  <?php do { ?>
                  <option value="<?php echo $row_kelas['kd_kelas']; ?>"><?php echo $row_kelas['kelas']; ?> - <?php echo $row_kelas['sub_kelas']; ?></option>
                  <?php } while ($row_kelas = mysql_fetch_assoc($kelas)); ?>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">NIS:</td>
                <td><input name="nis" type="text" id="nis" value="" readonly="readonly" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Nama:</td>
                <td><input name="nama" type="text" id="nama" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Jenis Kelamin:</td>
                <td><select name="jenis_kelamin">
                  <option value="L" <?php if (!(strcmp("L", ""))) {echo "SELECTED";} ?>>L</option>
                  <option value="P" <?php if (!(strcmp("P", ""))) {echo "SELECTED";} ?>>P</option>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tempat Lahir:</td>
                <td><input name="tempat_lahir" type="text" id="tempat_lahir" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tanggal Lahir:</td>
                <td><input name="tanggal_lahir" type="text" id="tanggal_lahir" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Agama:</td>
                <td><select name="agama">
                  <option value="Islam" <?php if (!(strcmp("Islam", ""))) {echo "SELECTED";} ?>>Islam</option>
                  <option value="Kristen" <?php if (!(strcmp("Kristen", ""))) {echo "SELECTED";} ?>>Kristen</option>
                  <option value="Katolik" <?php if (!(strcmp("Katolik", ""))) {echo "SELECTED";} ?>>Katolik</option>
                  <option value="Hindu" <?php if (!(strcmp("Hindu", ""))) {echo "SELECTED";} ?>>Hindu</option>
                  <option value="Budha" <?php if (!(strcmp("Budha", ""))) {echo "SELECTED";} ?>>Budha</option>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Alamat:</td>
                <td><input name="alamat" type="text" id="alamat" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Nama Ayah:</td>
                <td><input name="nama_ayah" type="text" id="nama_ayah" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Pekerjaan Ayah:</td>
                <td><input name="pekerjaan_ayah" type="text" id="pekerjaan_ayah" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Nama Ibu:</td>
                <td><input name="nama_ibu" type="text" id="nama_ibu" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Pekerjaan Ibu:</td>
                <td><input name="pekerjaan_ibu" type="text" id="pekerjaan_ibu" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Alamat Orang Tua:</td>
                <td><input name="alamat_ortu" type="text" id="alamat_ortu" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tlp. Orang Tua:</td>
                <td><input name="tlp_ortu" type="text" id="tlp_ortu" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Nama Wali:</td>
                <td><input name="nama_wali" type="text" id="nama_wali" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Pekerjaan Wali:</td>
                <td><input name="pekerjaan_wali" type="text" id="pekerjaan_wali" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Alamat Wali:</td>
                <td><input name="alamat_wali" type="text" id="alamat_wali" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tlp. Wali:</td>
                <td><input name="tlp_wali" type="text" id="tlp_wali" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Golongan Darah:</td>
                <td><select name="golongan_darah">
                  <option value="A" <?php if (!(strcmp("A", ""))) {echo "SELECTED";} ?>>A</option>
                  <option value="B" <?php if (!(strcmp("B", ""))) {echo "SELECTED";} ?>>B</option>
                  <option value="O" <?php if (!(strcmp("O", ""))) {echo "SELECTED";} ?>>O</option>
                  <option value="AB" <?php if (!(strcmp("AB", ""))) {echo "SELECTED";} ?>>AB</option>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Jumlah Saudara:</td>
                <td><input name="jum_saudara" type="text" id="jum_saudara" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Anak ke:</td>
                <td><input name="anak_ke" type="text" id="anak_ke" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Asal Sekolah:</td>
                <td><input name="asal_sekolah" type="text" id="asal_sekolah" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">No. STTB:</td>
                <td><input name="no_sttb" type="text" id="no_sttb" value="" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">&nbsp;</td>
                <td><input type="submit" value="Tambah Data Siswa" /></td>
              </tr>
            </table>
            <input type="hidden" name="MM_insert" value="form1" />
          </form>
      <p>&nbsp;</p></td>
    </tr>
    <tr>
    	<td colspan="2" id="footer">Copyright 2012</td>
    </tr>
    
</table></center>
</body>
</html>
<?php
mysql_free_result($kelas);

mysql_free_result($Recordset1);
?>
